package com.whp.yky.yky.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whp.yky.yky.entity.Borrow;
import com.whp.yky.yky.entity.DocumentEntity;
import com.whp.yky.yky.entity.poentity.Document;
import com.whp.yky.yky.response.ResponseData;
import com.whp.yky.yky.service.impl.BorrowServiceImpl;
import com.whp.yky.yky.service.impl.DocumentEntityServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author whp
 * @since 2021-04-21
 */

@CrossOrigin
@RestController
@RequestMapping("/borrow")
public class BorrowController {

    @Autowired
    private BorrowServiceImpl borrowService;

    @Autowired
    private DocumentEntityServiceImpl documentEntityService;

    @GetMapping("/borrowStart")
    public String borrowStart(@RequestParam Integer documentId, @RequestParam Integer userId){

        Borrow borrow = new Borrow();
        borrow.setDocumentId(documentId);
        borrow.setUserId(userId);
        borrow.setStatus("审核中");
        DocumentEntity documentEntity = new DocumentEntity();
        documentEntity.setSafeLevel("审核中");
        documentEntity.setId(documentId);
        System.out.println(documentEntity);
        borrowService.save(borrow);
        documentEntityService.updateById(documentEntity);
        return "已申请借阅，请等待管理员审核";
    }

    @PostMapping("/getShzBorrowList")
    public ResponseData getShzBorrowList(@RequestBody Borrow borrow ,@RequestParam Integer pageNum, @RequestParam Integer pageSize){

        ResponseData res = new ResponseData();
        res.setCode(20000);
        IPage<Borrow> pageList = this.borrowService.getShzBorrow(new Page( (long)pageNum,(long)pageSize),borrow);
        res.setData(pageList);
//        res.setTotal(documentServiceImpl.getDocumentInfo(document).size());

        return res;
    }



    @PostMapping("/getYshBorrowList")
    public ResponseData getYshBorrowList(@RequestBody Borrow borrow ,@RequestParam Integer pageNum, @RequestParam Integer pageSize){

        ResponseData res = new ResponseData();
        res.setCode(20000);
        IPage<Borrow> pageList = this.borrowService.getYshBorrow(new Page( (long)pageNum,(long)pageSize),borrow);
        res.setData(pageList);
//        res.setTotal(documentServiceImpl.getDocumentInfo(document).size());

        return res;
    }

    @PostMapping("/updateBorrow")
    public ResponseData getYshBorrowList(@RequestParam Integer id){

        ResponseData res = new ResponseData();
        Borrow borrow = new Borrow();
        borrow.setStatus("审核通过");
        borrow.setId(id);

        borrowService.updateById(borrow);
//        res.setTotal(documentServiceImpl.getDocumentInfo(document).size());
        res.setCode(20000);

        return res;
    }

    @DeleteMapping("/deletBorrowByID/{id}")
    @ApiOperation(value="删除用户", notes="根据id删除用户")
    public ResponseData deletBorrowByID(@PathVariable("id") int id){
        ResponseData res = new ResponseData();
        boolean result = borrowService.removeById(id);

        if (result){
            res.setCode(20000);
            res.setData("success");

        }else{
            res.setCode(50000);
            res.setData("fail");
        }
        return res;
    }

//    @PostMapping("/getBorrowList")
}

